package com.leetcode;

import com.leetcode.common.ListNode;

/**
 * 203. 移除链表元素
 * 迭代(使用虚拟头节点)
 */
public class Solution203 {

    public ListNode removeElements(ListNode head, int val) {
        ListNode dummyHead = new ListNode(-1, head);
        ListNode cur = dummyHead;
        while (cur.next != null) {
            if (cur.next.val == val) {
                cur.next = cur.next.next;
            } else {
                cur = cur.next;
            }
        }
        return dummyHead.next;
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(new int[]{1, 2, 6, 3, 4, 5});
        int val = 6;
        System.out.println(head);

        ListNode newHead = new Solution203().removeElements(head, val);
        System.out.println(newHead);

    }

}
